/**
 *******************************************************************************
 * @FileName  : ll_big_brdcst.h
 * @Author    : GaoQiu
 * @CreateDate: 2020-09-10
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */

#ifndef LL_BIG_BRDCST_H_
#define LL_BIG_BRDCST_H_

#include "stack/ble_types.h"
#include "stack/crypt/ccm.h"

#include "utils/fifo.h"
#include "utils/queue.h"

#define Ll_GET_BIG_HANDLE(pCtx)  ((pCtx) - pLlBigBrdcstCtx)
#define LL_GET_BIG_CTX(h)         &pLlBigBrdcstCtx[h]

/**
 * | payload number(4B) | DMA Length(4B) | LL BIS PDU |
 */
#define LL_BIS_PDU_OFFSET         4

typedef struct{
	uint8_t   ChnM[5];
	uint16_t  instant;
}LlBigChnMapInd_t;

typedef struct{
	uint8_t   reason;
	uint16_t  instant;
}LlBigTerminateInd_t;

typedef struct ALIGN(1){
	uint8_t BIG_Offset;
}BIGInfo_t;

typedef struct{
	uint8_t              InUsed;
	uint8_t              state;
	uint8_t              handle;
	uint8_t              NumBIS;

	uint32_t             SDUInterval;   /*!< SDU interval in us; rang of 0x000100 - 0x0FFFFF */
	uint32_t             ISOInterval;   /*!< ISO interval in 1.25ms; rang of 5ms - 4s */

	uint16_t             MaxSDU;
	uint16_t             MaxPDU;

	uint8_t              NSE;
	uint8_t              BN;
	uint8_t              IRC;
	uint8_t              PTO;

	uint8_t              phy;           /*!< bits field */
	uint8_t              packing;       /*!< Sequential Or Interleaved */
	uint8_t              framing;       /*!< Unframed Or framed */
	uint8_t              encryption;
	uint8_t              BrdcstCode[16]; /*!< big-endian */

	uint64_t             ChnMap;
	uint8_t              ChnMapTbl[40];
	uint8_t              NumUsedChn;
	uint8_t              LastUnmapedChnIndex;
	uint8_t              UnmapedChnIndex;
	uint8_t              UsedPHY;
	uint8_t              PhyOpt;               /*!< coded PHY option: S2/S8 */
	uint8_t              CSSN;
	uint8_t              CSTF;
	uint8_t              BigTermEn     :1;
	uint8_t              BIgChnMapUpdEn:1;

	uint32_t             SeedAccessCode;
	uint32_t             BaseCRCInit;
	uint32_t             BigAccessCode;     /*!< BIG Control PDU access code */
	uint32_t             BigCRCInit;        /*!< BIG Control PDU CRC init = BaseCRCInit<<8 | 0 */

	uint32_t             BigEventCnt;       /*!< BIG valid bits[0-38] */
	uint32_t             AnchorPoint;       /*!< BIG Event Anchor Point in tick */
	uint32_t             BisSpacing;        /*!< BIS Spacing in us */
 	uint32_t             BisSubInterval;    /*!< BIS sub interval in us */
 	uint32_t             BigCtrlPduOffset;  /*!< in us */
 	uint32_t             SubEventLen;       /*!< Sub event duration time in us */

 	uint8_t              GIV[8];
 	uint8_t              GSKD[16];
 	uint16_t             SK[16];
 	ccm_ctx_t            EncCtx;

 	LlBigTerminateInd_t  TermInd;
 	LlBigChnMapInd_t     NewChnM;

 	void                 *pBisCtxTbl;
#if 0
 	uint8_t              *pBisIndexTbl;        /*!< BIS arrangement table for packing */
 	uint8_t               BisIndexNum;
 	uint8_t               BisCurIndex;
#endif
}LlBigBrdcstCtx_t;

typedef struct LlBisBrdcstCtx_s{
	LlBigBrdcstCtx_t        *pBigCtx;
	fifo_t                   TxFifo;

	uint32_t                 AccessCode;
	uint32_t                 CRCInit;      /*!< BIG Control PDU CRC init = BaseCRCInit<<8 | BIS_Index */

	uint8_t                  SubEventCnt;

	uint16_t                 ChanId;
	uint16_t                 LastUsedPrn;
	uint16_t                 LastUsedChnIndex;
	uint16_t                 PrnSubEvent_lu;
	uint16_t                 PrnSubEvent_se;
	uint16_t                 SubEventIndex;

	ccm_ctx_t                EncCtx;
}LlBisBrdcstCtx_t;

extern LlBigBrdcstCtx_t  pLlBigBrdcstCtx;

#endif /* LL_BIG_BRDCST_H_ */
